package com.joint.web.action.mp;

import com.fz.us.base.bean.BaseEnum.StateEnum;
import com.fz.us.base.bean.Pager;
import com.joint.base.entity.Company;
import com.joint.base.entity.LocationEntity;
import com.joint.base.entity.Users;
import com.joint.base.entity.system.Locations;
import com.joint.base.service.LocationEntityService;
import com.joint.base.service.UsersService;
import com.joint.core.entity.ProInfo;
import com.joint.core.entity.ProSignIn;
import com.joint.core.service.ProInfoService;
import com.joint.core.service.ProSignInService;
import com.joint.web.action.BaseController;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xh on 2015/3/2.
 */
@Controller("mpProsign")
@RequestMapping("/mp")
public class AjaxProsigninController extends BaseController {

    @Resource
    private ProSignInService proSignInService;
    @Resource
    private ProInfoService proInfoService;
    @Resource
    private UsersService usersService;
    @Resource
    private LocationEntityService locationEntityService;

    protected ProSignIn proSignIn;

    private String keyId;
    private String proInfoId;

    private String address;
    private int status;
    private String start;
    private String end;
    private String userId;
    private List<Users> usersList;

    private String proArea;




    public String view(){
        return "view";
    }

    public String calendar(){
        return "calendar";
    }

    public String read(){
        //LogUtil.info("keyId:" + keyId);
        if(StringUtils.isNotEmpty(keyId)){
            proSignIn = proSignInService.get(keyId);
        }
        ProInfo proInfo = proSignIn.getProInfo();
        proArea = proInfo.getProvince()+proInfo.getCity()+proInfo.getCounty();
        Locations locations=locationsService.get(proSignIn.getLocationId());
        LocationEntity locationEntity=locationEntityService.getLocationByTargetId(locations.getId());
        if(locationEntity!=null){
            address=locationsService.getAddress(locations);
        }else{
            address=proSignIn.getAddress();
        }
        return "read";
    }

    public String input(){
        ProInfo proInfo;
        if (StringUtils.isNotEmpty(proInfoId)){
            proInfo = proInfoService.get(proInfoId);
        }else {
            return ajaxHtmlCallback("404", "所属项目信息表丢失！","操作状态");
        }

        if (StringUtils.isNotEmpty(keyId)){
            proSignIn = proSignInService.get(keyId);
        }else{
            proSignIn = new ProSignIn();
            proSignIn.setProInfo(proInfo);
        }
        proArea = proInfo.getProvince()+proInfo.getCity()+proInfo.getCounty();
        return  "input";
    }


    public String save(){
        ProInfo proInfo;
        if (StringUtils.isNotEmpty(proInfoId)){
            proInfo = proInfoService.get(proInfoId);
        }else {
            return ajaxHtmlCallback("404", "所属项目信息表丢失！","操作状态");
        }

        //LogUtil.info("keyId:"+keyId);
        if(StringUtils.isNotEmpty(keyId)){
            proSignIn = proSignInService.get(keyId);
        }else{
            proSignIn = new ProSignIn();
            proSignIn.setCreater(usersService.getLoginInfo());
        }

        proSignIn.setProInfo(proInfo);
        proSignIn.setName(proInfo.getName());

        proSignIn.setAddress(address);
        proSignIn.setStatus(status);

        proSignIn.setCompany(usersService.getCompanyByUser());
        proSignInService.updateAndEnable(proSignIn);
        return ajaxHtmlCallback("200", "保存成功！","操作状态");
    }

    public String delete(){
        if(StringUtils.isEmpty(keyId)){
            return ajaxHtmlCallback("404", "请选择客户信息！","操作状态");
        }
        ProSignIn proSignIn = proSignInService.get(keyId);
        proSignIn.setState(StateEnum.Delete);
        proSignInService.update(proSignIn);
        return ajaxHtmlCallback("200", "删除成功！","操作状态");
    }


    public String map(){
        Company company=usersService.getCompanyByUser();
        pager= usersService.findByPagerAndCompany(new Pager(),null,company,new StateEnum[]{StateEnum.Enable});
        usersList= (List<Users>) pager.getList();
        return "map";
    }


    @Override
    public String getKeyId() {
        return keyId;
    }

    @Override
    public void setKeyId(String keyId) {
        this.keyId = keyId;
    }

    public String getProInfoId() {
        return proInfoId;
    }

    public void setProInfoId(String proInfoId) {
        this.proInfoId = proInfoId;
    }

    public ProSignIn getProSignIn() {
        return proSignIn;
    }

    public void setProSignIn(ProSignIn proSignIn) {
        this.proSignIn = proSignIn;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getProArea() {
        return proArea;
    }

    public void setProArea(String proArea) {
        this.proArea = proArea;
    }

    public String getStart() {
        return start;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public String getEnd() {
        return end;
    }

    public void setEnd(String end) {
        this.end = end;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public List<Users> getUsersList() {
        return usersList;
    }

    public void setUsersList(List<Users> usersList) {
        this.usersList = usersList;
    }
}
